package com.example.mystoryapp2.ui.story

import androidx.lifecycle.MutableLiveData
import com.example.mystoryapp2.adapter.RecordsListAdapter
import com.example.mystoryapp2.contexts.AppContext
import com.example.mystoryapp2.contexts.StoryContext
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.data.network.model.PageModel
import com.example.mystoryapp2.data.network.model.StoryModel
import com.example.mystoryapp2.data.network.model.StoryOperationModel
import com.example.mystoryapp2.domain.Category
import com.example.mystoryapp2.domain.Page
import com.example.mystoryapp2.domain.RecordStatic
import com.example.mystoryapp2.domain.Story
import com.example.mystoryapp2.ui.base.BaseViewModel

class StoryDescriptionViewModel : BaseViewModel() {
    var adapter = MutableLiveData(RecordsListAdapter(ArrayList()))
    var story: MutableLiveData<Story> = MutableLiveData(Story())
    val categoryName: MutableLiveData<String> =
        MutableLiveData(Category.getUnKnow().getCategoryName())

    fun onPublish(res: (Boolean) -> Unit) {
        story.value?.let {
            StoryOperationModel.publish(it, object : ServiceCreator.ICallBack<Boolean> {
                override fun onSuccess(result: Boolean) {
                    res(result)
                }
            })
        }
    }

    fun onCollect(res: (Boolean) -> Unit) {
        StoryOperationModel.collect(buildOpeStory(), object : ServiceCreator.ICallBack<Boolean> {
            override fun onSuccess(result: Boolean) {
                res(result)
            }
        })
    }

    fun onDelete(res: (Boolean) -> Unit) {
        story.value?.let {
            StoryOperationModel.delete(it, object : ServiceCreator.ICallBack<Boolean> {
                override fun onSuccess(result: Boolean) {
                    res(result)
                }
            })
        }
    }

    fun checkCollect(back: (Boolean) -> Unit) {
        story.value?.let {
            StoryOperationModel.checkCollect(
                buildOpeStory(),
                object : ServiceCreator.ICallBack<Boolean> {
                    override fun onSuccess(res: Boolean) {
                        back(res)
                    }
                })
        }
    }

    fun initStoryInfo(suc: () -> Unit, fail: () -> Unit) {
        StoryModel.getStoryById(StoryContext.storyId, object : ServiceCreator.ICallBack<Story> {
            override fun onSuccess(res: Story) {
                story.value = res
                buildCategoryStr()
                initPageStatic()
                suc()
            }

            override fun onFailed() {
                super.onFailed()
                fail()
            }
        })
    }

    fun checkCollected(f: (Boolean) -> Unit) {
        StoryOperationModel.checkCollect(
            buildOpeStory(),
            object : ServiceCreator.ICallBack<Boolean> {
                override fun onSuccess(res: Boolean) {
                    f(res)
                }
            })
    }


    fun buildCategoryStr() {
        categoryName.value = StoryModel.buildCategoryStr(story.value?.getCategoryId())
    }

    fun getFirstPage(b: (Int) -> Unit) {
        buildOpeStory().let {
            PageModel.loadLastReadPage(
                it,
                object : ServiceCreator.ICallBack<Int> {
                    override fun onSuccess(res: Int) {
                        if(res!=-1){
                            b(res)
                        }else{
                            loadFirstPageOfStory(b)
                        }
                    }
                    override fun onFailed() {
                        loadFirstPageOfStory(b)
                    }
                })
        }
    }

    fun loadFirstPageOfStory(b:(Int)->Unit){
        PageModel.getFirst(
            StoryContext.storyId,
            object : ServiceCreator.ICallBack<Page> {
                override fun onSuccess(res: Page) {
                    b(res.pageId!!)
                }

                override fun onFailed() {
                    b(-1)
                }
            })
    }

    /**
     * 检查用户是否为当前页面的作者
     */
    fun checkIsAuthor(f: (Boolean) -> Unit) {
        f(
            (story.value?.getAuthorId() == AppContext.userInfo.userId)
                    && story.value?.getIsPublished() == true && AppContext.isAuthor()
        )
    }

    private fun buildOpeStory(): Story {
        val story1 = Story()
        story1.setStoryId(story.value?.getStoryId())
        story1.setAuthorId(AppContext.userInfo.userId)
        return story1
    }

    private fun initPageStatic() {
        PageModel.getReadStatic(StoryContext.storyId, object : ServiceCreator
        .ICallBack<List<RecordStatic>> {
            override fun onSuccess(res: List<RecordStatic>) {
                if (res != null) {
                    adapter.value?.setList(res as ArrayList<RecordStatic>)
                }
            }
        })
    }
}